import { mock } from 'mockjs'

const customers = Array.from({ length: 50 }).map((_, index) => ({
  id: `${index + 1}`,
  customerCode: `C${String(index + 1).padStart(5, '0')}`,
  customerName: `客户${index + 1}`,
  shortName: `客户${index + 1}`,
  customerType: ['U8', '电池箔', '铜铝复合', '纯铝', '再生铝'][index % 5],
  responsiblePerson: `负责人${index + 1}`,
  responsiblePhone: `1381234${String(index + 1).padStart(4, '0')}`,
  responsibleEmail: `contact${index + 1}@example.com`,
  address: `地址${index + 1}`,
  contacts: [
    {
      id: `contact-${index + 1}-1`,
      contactName: `联系人${index + 1}-1`,
      contactPhone: `1391234${String(index + 1).padStart(4, '0')}`,
      email: `contact${index + 1}-1@example.com`,
      position: '经理',
      status: 'normal'
    }
  ],
  status: ['pre-sales', 'normal', 'deleted'][index % 3],
  remark: `备注${index + 1}`,
  createTime: new Date().toISOString(),
  updateTime: new Date().toISOString()
}))

export const customerApis = [
  // 获取客户列表
  {
    url: '/api/customers',
    method: 'get',
    response: ({ query }) => {
      const { page = 1, pageSize = 10, keyword, customerType, status } = query
      
      // 筛选数据
      let filtered = [...customers]
      if (keyword) {
        filtered = filtered.filter(item => 
          item.customerName.includes(keyword) || 
          item.customerCode.includes(keyword) ||
          item.responsiblePerson.includes(keyword) ||
          item.contacts.some(contact => contact.contactName.includes(keyword))
        )
      }
      if (customerType) {
        filtered = filtered.filter(item => item.customerType === customerType)
      }
      if (status) {
        filtered = filtered.filter(item => item.status === status)
      }
      
      // 分页
      const start = (page - 1) * pageSize
      const end = start + pageSize
      const pageData = filtered.slice(start, end)
      
      return {
        code: 200,
        message: '获取成功',
        data: {
          items: pageData,
          total: filtered.length
        }
      }
    }
  },
  // 获取客户详情
  {
    url: '/api/customers/:id',
    method: 'get',
    response: ({ query }) => {
      const customer = customers.find(item => item.id === query.id)
      if (customer) {
        return {
          code: 200,
          message: '获取成功',
          data: customer
        }
      } else {
        return {
          code: 404,
          message: '客户不存在'
        }
      }
    }
  },
  // 创建客户
  {
    url: '/api/customers',
    method: 'post',
    response: ({ body }) => {
      const newCustomer = {
        ...body,
        id: `${customers.length + 1}`,
        createTime: new Date().toISOString(),
        updateTime: new Date().toISOString()
      }
      customers.unshift(newCustomer)
      return {
        code: 200,
        message: '创建成功',
        data: newCustomer
      }
    }
  },
  // 更新客户
  {
    url: '/api/customers/:id',
    method: 'put',
    response: ({ body }) => {
      const index = customers.findIndex(item => item.id === body.id)
      if (index !== -1) {
        customers[index] = {
          ...customers[index],
          ...body,
          updateTime: new Date().toISOString()
        }
        return {
          code: 200,
          message: '更新成功',
          data: customers[index]
        }
      } else {
        return {
          code: 404,
          message: '客户不存在'
        }
      }
    }
  },
  // 删除客户
  {
    url: '/api/customers/:id',
    method: 'delete',
    response: ({ query }) => {
      const index = customers.findIndex(item => item.id === query.id)
      if (index !== -1) {
        customers.splice(index, 1)
        return {
          code: 200,
          message: '删除成功'
        }
      } else {
        return {
          code: 404,
          message: '客户不存在'
        }
      }
    }
  },
  // 获取客户类型枚举
  {
    url: '/api/enums/customerTypes',
    method: 'get',
    response: () => {
      return {
        code: 200,
        message: '获取成功',
        data: [
          { code: 'U8', name: 'U8' },
          { code: 'BATTERY', name: '电池箔' },
          { code: 'COPPER_ALUMINUM', name: '铜铝复合' },
          { code: 'PURE_ALUMINUM', name: '纯铝' },
          { code: 'RECYCLED_ALUMINUM', name: '再生铝' }
        ]
      }
    }
  }
]

export default customers